package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

public class NameInUseDialogFragment extends DialogFragment {

    /**
     * TODO need a interface name confirm
     */

    public interface NameInUseDialogCallback {
        /**
         * Callback when user select cancel
         */
        void onDuplicateFileSkip(boolean applyAllItems);

        /**
         * Callback when user select replace
         */
        void onDuplicateFileReplace(boolean applyAllItems);

        /**
         * Callback when user select rename
         */
        void onDuplicateFileRename(boolean applyAllItems);
    }

    private FileRecord mCurRecord;
    private NameInUseDialogCallback mCallback;
    private View mRootView;

    public static NameInUseDialogFragment getInstance(FileRecord record, NameInUseDialogCallback callback) {
        NameInUseDialogFragment fg = new NameInUseDialogFragment();
        fg.mCurRecord = record;
        fg.mCallback = callback;

        return fg;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Activity a = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(a);
        mRootView = a.getLayoutInflater().inflate(R.layout.dialog_text_checkbox_common, null);
        builder.setView(mRootView);
        TextView tv = (TextView) mRootView.findViewById(R.id.dialog_main_text);

        int titleResId;
        int tvResId;

        if (mCurRecord.isDirectory()) {
            titleResId = R.string.folder_name_in_use;
            tvResId = R.string.folder_already_exist;
        } else {
            titleResId = R.string.file_name_in_use;
            tvResId = R.string.file_already_exist;
        }

        builder.setTitle(titleResId);
        tv.setText(String.format(getString(tvResId), mCurRecord.getName()));

        final CheckBox applyAllItems = (CheckBox) mRootView.findViewById(R.id.dialog_checkbox);
        applyAllItems.setText(R.string.apply_to_all_items);

        applyAllItems.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.REPLACE_POPUP,
                        SamsungAnalyticsLog.Event.APPLY_TO_ALL_ITEMS, ((CheckBox) arg0).isChecked() ? 1L : 0L, null);
            }
        });

        builder.setPositiveButton(getString(R.string.menu_rename), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (mCallback != null) {
                    mCallback.onDuplicateFileRename(applyAllItems.isChecked());
                    SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.REPLACE_POPUP, SamsungAnalyticsLog.Event.RENAME, null);
                }
            }
        });

        builder.setNegativeButton(getString(R.string.replace), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (mCallback != null) {
                    mCallback.onDuplicateFileReplace(applyAllItems.isChecked());
                    SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.REPLACE_POPUP, SamsungAnalyticsLog.Event.REPLACE, null);
                }
            }
        });

        builder.setNeutralButton(getString(R.string.skip), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (mCallback != null) {
                    mCallback.onDuplicateFileSkip(applyAllItems.isChecked());
                    SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.REPLACE_POPUP, SamsungAnalyticsLog.Event.SKIP, null);
                }
            }
        });

        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_MENU;
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;

    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (mCallback != null) {
            mCallback.onDuplicateFileSkip(true);
        }

        super.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Be careful, onDismiss will call back from android framework after
        // screen orientation things.
        super.onDismiss(dialog);
    }

    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null && !fm.isDestroyed()) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }

    public void updateView(int minHeight) {
        if (mRootView != null) {
            mRootView.setMinimumHeight(minHeight);
            mRootView.invalidate();
        }
    }
}
